<template>
  <div class="login-page">
    <div class="back">
      <!-- $router.back() 返回上一次访问路由，forward go -->
      <i class="iconfont icon-esay-close" @click="$router.back()"></i>
    </div>
    <h3 class="title">短信登录</h3>
    <!-- 表单 -->
    <van-form ref="userRef" validate-trigger="onChange">
      <van-field
        v-model="userForm.mobile"
        placeholder="请输入手机号"
        :rules="rules.mobile"
        name="mobile"
      />
      <!-- 通过 validator 进行函数校验 -->
      <van-field
        placeholder="请输入验证码"
        v-model="userForm.code"
        :rules="rules.code"
      >
        <template #button>
          <span  class="send" @click="send">
            {{ count === 0 ? '发送验证码' :`${count}后发送验证码` }}
        </span>
        </template>
      </van-field>
    </van-form>
    <van-button @click="login">登录</van-button>
  </div>
</template>

<script>
import { loginAPI, sendVerify } from '@/api/user'

export default {
  name: 'LoginPage',
  data () {
    return {
      userForm: {
        mobile: '17347306728',
        code: '246810'
      },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不对' }
        ],
        code: [
          { required: true, message: '请输入手机号' },
          { pattern: /^\d{6}$/, message: '验证码是6个数字' }
        ]
      },
      count: 0
    }
  },
  beforeDestroyR () {
    if (this.timer) clearInterval(this.timer)
  },
  methods: {
    async login () {
      await this.$refs.userRef.validate()
      const [err, res] = await loginAPI(this.userForm)
      if (err) return this.$toast.fail('登录失败')
      this.$store.commit('user/SET_TOKEN', {
        current_token: res.data.data.token,
        refresh_token: res.data.data.refresh_token
      })
      const returnUrl = this.$route.query.returnUrl
      delete this.$route.query.returnUrl
      this.$router.replace(returnUrl || '/')
    },
    async send () {
      if (this.count > 0) return
      const [err] = await sendVerify(this.userForm.mobile)
      if (err) return this.$toast.fail('发送失败')
      if (this.timer) clearInterval(this.timer)
      this.count = 60
      this.timer = setInterval(() => {
        this.count--
        if (!this.count) {
          clearInterval(this.timer)
        }
      }, 1000)
    }
  }
}
</script>

  <style scoped lang="less">
  .send {
    font-size: 12px;
    color: #A5A6AB;
  }
.login-page {
  padding: 0 32px;
  .back {
    height: 60px;
    display: flex;
    align-items: center;
    .icon-esay-close {
      font-size: 20px;
      color: #ccc;
      position: relative;
      left: -15px;
    }
  }
  .title {
    font-size: 22px;
    line-height: 1;
    padding: 30px 0;
  }
  .van-cell {
    padding: 20px 0;
    &::after {
      left: 0;
      right: 0;
    }
  }
  .van-button {
    width: 100%;
    margin-top: 40px;
    height: 50px;
    color: #fff;
    font-size: 16px;
    border: none;
    background: linear-gradient(to right, #ff9999, #ffa179);
  }
}
</style>
